import sys 
import pygame 
from   settings import Settings
from   ship import Ship
from   bullet import Bullet
from   target import Target

class AlienInvsion:
    test = 0
    #管理游戏资源和行为的类
    def __init__(self):
        #初始化游戏并创建游戏资源
    #初始化背景设置
        pygame.init()    
    #title
        pygame.display.set_caption(Settings().title)    
    #设置背景色    
        self.bg_color = Settings().bg_color       
    #帧率设置
        self.clock = pygame.time.Clock()
        self.fps = Settings().fps  # 帧
    #创建一个显示窗口,用一个元组指定窗口大小
        self.screen =pygame.display.set_mode((Settings().screen_width,Settings().screen_height))      
    #引入飞船,子弹，攻击目标等对象
        self.ship = Ship(self)
        self.bullet = Bullet(self)
        # self.target = Target(self)

        #上下左右BABA and fire
    def check_move(self, event, bool):
        if event.key in (pygame.K_RIGHT, pygame.K_d):
            self.ship.moving_right = bool
        elif event.key in (pygame.K_DOWN, pygame.K_s):
            self.ship.moving_down = bool
        elif event.key in (pygame.K_LEFT, pygame.K_a):
            self.ship.moving_left = bool
        elif event.key in (pygame.K_UP, pygame.K_w):
            self.ship.moving_up = bool

    def check_fire(self,event,bool):
        if event.key ==pygame.K_SPACE:
           if self.bullet.fire == bool:
            self.bullet.fire = bool
           else:
            self.bullet.rect.midtop = self.ship.rect.midtop
            self.bullet.fire = bool 

    #键盘响应
    def _check_events(self):
        #监视键盘和鼠标事件
        for event in pygame.event.get():        #事件循环，使用函数pygame.event.get()来访问事件
            if event.type == pygame.QUIT :
                sys.exit()
            elif event.type == pygame.KEYDOWN:  #按键按下情况
                #快捷键退出游戏
                if event.key in(pygame.K_q,pygame.K_0) :
                    sys.exit()
                #移动
                self.check_move(event,True)
                self.check_fire(event,True)

            elif event.type == pygame.KEYUP:    #按键松开情况
                #停止移动
                self.check_move(event,False)
                
            else:
                print(event.type)

    def moving_bullet(self):
        if self.bullet.fire:
            self.bullet.rect.y-=self.bullet.speed
            if self.bullet.rect.y<=0:
                self.bullet =Bullet(self)
            
    #持续移动
    def moving_ship(self):
        if self.ship.moving_right:
            self.ship.rect.x += self.ship.speed
            if self.ship.rect.x >= Settings().screen_width-self.ship.weight:
                self.ship.rect.x =Settings().screen_width-self.ship.weight

        if self.ship.moving_left:
            self.ship.rect.x -= self.ship.speed
            if self.ship.rect.x <=0:
                self.ship.rect.x = 0

        if self.ship.moving_up:
            self.ship.rect.y -= self.ship.speed
            if self.ship.rect.y <= 0:
                self.ship.rect.y=0

        if self.ship.moving_down:
            self.ship.rect.y += self.ship.speed
            if self.ship.rect.y >= Settings().screen_height-self.ship.height:
                self.ship.rect.y = Settings().screen_height-self.ship.height



    def _update_screen(self):
        #每次循环时都会重绘屏幕
        self.screen.fill(Settings().bg_color)     #fill()用于处理surgace，只接受一个实参
        self.moving_ship()
        self.moving_bullet()
        self.bullet.blitme()
        self.ship.blitme()
        
        #让最近绘制的屏幕可见
        pygame.display.flip()       #每执行一次while循环都会绘制一个空屏幕，并擦去旧屏幕，从而制造平移滑动的效果


        
        
    def run_game(self):
        #开始游戏的主循环
        while True:
            self.clock.tick(self.fps)
            #更新屏幕
            self._update_screen()
            #键盘响应
            self._check_events()
            

if __name__ == '__main__':
    #创建游戏实例并运行游戏
    ai = AlienInvsion()
    ai.run_game()
